package com.huage.courseplatform.satoken.controller;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huage.courseplatform.framework.annotation.RepeatSubmit;
import com.huage.courseplatform.framework.api.AbstractController;
import com.huage.courseplatform.framework.model.param.PagePARAM;
import com.huage.courseplatform.framework.utils.BeanCopyUtils;
import com.huage.courseplatform.framework.utils.ResultUtils;
import com.huage.courseplatform.satoken.model.entity.User;
import com.huage.courseplatform.satoken.model.param.PasswordUpdatePARAM;
import com.huage.courseplatform.satoken.model.param.UserAndRolePARAM;
import com.huage.courseplatform.satoken.model.param.UserPARAM;
import com.huage.courseplatform.satoken.model.vo.UserVO;
import com.huage.courseplatform.satoken.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/user")
@Api(tags = "用户管理接口")
@Slf4j
public class UserController extends AbstractController<User,UserVO, UserPARAM>{

    @Resource
    private UserService userService;


    @SaCheckRole("ROLE_ADMIN")
    @RepeatSubmit
    @ApiOperation(value = "添加用户(密码默认在设置表)")
    @PostMapping(value = "/add")
    public ResultUtils<UserVO> add(@RequestBody @Validated({Insert.class}) UserAndRolePARAM userAndRolePARAM){
        if(StringUtils.isBlank(userAndRolePARAM.getEmail()) && StringUtils.isBlank(userAndRolePARAM.getPhone())){
            Assert.isTrue(false,"邮箱和手机号必须填写其中一项");
        }
        User user = BeanCopyUtils.copyProperties(userAndRolePARAM,User.class);
        user = userService.addUserAndRole(user, userAndRolePARAM.getRoleList());
        UserVO userVO = entityToDto(user);

        return ResultUtils.success(userVO);
    }

    @SaCheckRole("ROLE_ADMIN")
    @RepeatSubmit
    @ApiOperation(value = "更新用户(管理员权限)")
    @PutMapping(value = "/update")
    public ResultUtils<UserVO> update(@RequestBody @Validated({Update.class}) UserAndRolePARAM userAndRolePARAM){
        User user = BeanCopyUtils.copyProperties(userAndRolePARAM,User.class);
        user = userService.updateUserAndRole(user, userAndRolePARAM.getRoleList());
        UserVO userVO = entityToDto(user);

        return ResultUtils.success(userVO);
    }

    @SaCheckRole("ROLE_ADMIN")
    @ApiOperation(value = "根据id删除用户")
    @DeleteMapping(value = "/delete/{id}")
    public ResultUtils<String> deleteById(@PathVariable String id) {
        userService.removeUserAndRoleById(id);
        return ResultUtils.success(id);
    }


    @ApiOperation(value = "根据id查询用户")
    @GetMapping(value = "/{id}")
    public ResultUtils<UserVO> getById(@PathVariable String id) {
        return ResultUtils.success(userService.getUserAndRoleById(id));
    }

    @RepeatSubmit
    @ApiOperation(value = "根据id修改个人信息(普通用户)")
    @PutMapping(value = "/updateById")
    public ResultUtils<UserVO> updateById(@RequestBody @Validated({Update.class} )UserPARAM userPARAM){
        User user = paramToEntity(userPARAM);
        userService.saveOrUpdate(user);
        UserVO userVO = entityToDto(user);
        return ResultUtils.success(userVO);
    }

    @ApiOperation(value = "修改密码")
    @PutMapping(value = "/updatePassword")
    public ResultUtils<?> updatePassword(@Validated @RequestBody PasswordUpdatePARAM passwordUpdatePARAM) {
        userService.updatePassword(passwordUpdatePARAM.getUserId(),passwordUpdatePARAM.getOldPassword(),passwordUpdatePARAM.getNewPassword());
        return ResultUtils.success("修改成功,请重新登陆");
    }

    @SaCheckRole("ROLE_ADMIN")
    @ApiOperation(value = "密码重置")
    @PutMapping(value = "/resetPassword/{id}")
    public ResultUtils<?> resetPassword(@PathVariable String id) {
        userService.resetPassword(id);
        return ResultUtils.success("重置成功，重置后的密码为:123456，请重新登陆");
    }


    @SaCheckRole("ROLE_ADMIN")
    @ApiOperation(value = "分页查询")
    @PostMapping(value = "/page")
    public ResultUtils<Page<UserVO>> getByPage(@Validated @RequestBody PagePARAM pagePARAM) throws IllegalAccessException {
        Page<UserVO> page = new Page<UserVO>(pagePARAM.getPageNum(),pagePARAM.getPageSize());
        QueryWrapper<UserVO> qw =  super.getQueryWrapperByPageParam(pagePARAM);
        return ResultUtils.success(this.userService.getUserAndRolesPage(page,qw));
    }




}
